package com.caifu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import com.caifu.excel.ExcelStaffinfo;
import com.caifu.mapper.YlBasicDataMapper;
import com.caifu.mapper.YlNursingRecordMapper;
import com.caifu.mapper.YlStaffInfoMapper;
import com.caifu.pojo.SysUser;
import com.caifu.pojo.YlBasicData;
import com.caifu.pojo.YlNursingRecord;
import com.caifu.pojo.YlStaffInfo;
import com.caifu.service.YlStaffInfoService;
import com.caifu.util.AesMyUtil;
import com.caifu.util.Constance;
import com.caifu.util.Util;
import com.caifu.vo.YlStaffInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author tww
 * ClassName: YlStaffInfoServiceImpl.java、
 * createTime: 2020年11月09日 10:04:08
 **/
@Service
public class YlStaffInfoServiceImpl implements YlStaffInfoService {

    @Resource
    private YlStaffInfoMapper ylStaffInfoMapper;

    @Resource
    private YlNursingRecordMapper ylNursingRecordMapper;

    @Override
    public PageInfo<YlStaffInfoVo> getStaffInfoList(YlStaffInfoVo ylStaffInfoVo) throws Exception {
        //年龄条件 0 小于35 1 36-45 2 大于45
        String age1 = "0";
        String age2 = "1";
        String age3 = "2";
        if (age1.equals(ylStaffInfoVo.getAgelabal())) {
            ylStaffInfoVo.setAgelabal("AND ysi.age <= 35");
        } else if (age2.equals(ylStaffInfoVo.getAgelabal())) {
            ylStaffInfoVo.setAgelabal(" AND ysi.age BETWEEN 36 AND 45");
        } else if (age3.equals(ylStaffInfoVo.getAgelabal())) {
            ylStaffInfoVo.setAgelabal("AND ysi.age > 45");
        }
        if (Util.isNotNull(ylStaffInfoVo.getIdCard())) {
            ylStaffInfoVo.setIdCard(AesMyUtil.Encrypt(ylStaffInfoVo.getIdCard()));
        }
        if (Util.isNotNull(ylStaffInfoVo.getPhone())) {
            ylStaffInfoVo.setPhone(AesMyUtil.Encrypt(ylStaffInfoVo.getPhone()));
        }
        PageHelper.startPage(ylStaffInfoVo.getCurrentPage(), ylStaffInfoVo.getPageSize());
        List<YlStaffInfoVo> staffInfoList = ylStaffInfoMapper.getStaffInfoList(ylStaffInfoVo);
        for (YlStaffInfoVo staffInfoVo : staffInfoList) {
            staffInfoVo.setPhone(AesMyUtil.Decrypt(staffInfoVo.getPhone()));
        }
        return new PageInfo<>(staffInfoList);
    }

    @Override
    public Result addStaffInfo(YlStaffInfo ylStaffInfo, SysUser user) throws Exception {
        if (Util.isNull(ylStaffInfo.getStaffNo())) {
            //添加
            //校验身份证是否存在
            String encryptIdCard = AesMyUtil.Encrypt(ylStaffInfo.getIdCard());
            if (checkIdCard(encryptIdCard)) {
                return ResultUtil.error(500, "身份证号已存在");
            }
            ylStaffInfo.setIdCard(encryptIdCard);
            ylStaffInfo.setPhone(AesMyUtil.Encrypt(ylStaffInfo.getPhone()));
            ylStaffInfo.setCreateTim(new Date());
            ylStaffInfo.setStaffState("1");
            ylStaffInfo.setPensionNo(user.getCurrentPensionNo());
            ylStaffInfo.setTellerNo(String.valueOf(user.getUserNo()));
            ylStaffInfoMapper.insert(ylStaffInfo);
        } else {
            //修改
            //检验身份证是否存在
            YlStaffInfo staffInfo = ylStaffInfoMapper.selectById(ylStaffInfo.getStaffNo());
            String encryptIdCard = AesMyUtil.Encrypt(ylStaffInfo.getIdCard());
            if (Util.isNotNull(staffInfo)) {
                if (!ylStaffInfo.getIdCard().equals(staffInfo.getStaffNo()) && checkIdCard(encryptIdCard)) {
                    return ResultUtil.error(500, "身份证号已存在");
                }
                ylStaffInfo.setIdCard(encryptIdCard);
                ylStaffInfo.setPhone(AesMyUtil.Encrypt(ylStaffInfo.getPhone()));
                ylStaffInfo.setCreateTim(new Date());
                ylStaffInfo.setStaffState("1");
                ylStaffInfo.setPensionNo(user.getCurrentPensionNo());
                ylStaffInfo.setTellerNo(String.valueOf(user.getUserNo()));

                ylStaffInfoMapper.updateById(ylStaffInfo);
            }
        }
        return ResultUtil.success(200, "保存成功");
    }

    /**
     * 校验身份证是否存在
     *
     * @param idCard
     * @return
     */
    private boolean checkIdCard(String idCard) {
        QueryWrapper<YlStaffInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID_CARD", idCard).eq("STAFF_STATE", 1);
        int count = ylStaffInfoMapper.selectCount(queryWrapper);
        return count > 1 ? true : false;
    }

    @Override
    public YlStaffInfo getStaffInfoById(String staffNo) throws Exception {
        YlStaffInfo staffInfo = ylStaffInfoMapper.selectById(staffNo);
        staffInfo.setIdCard(AesMyUtil.Decrypt(staffInfo.getIdCard()));
        staffInfo.setPhone(AesMyUtil.Decrypt(staffInfo.getPhone()));
        return staffInfo;
    }

    @Override
    public Map<String, Object> deleteById(String staffNo) {
        Map<String, Object> map = new HashMap<>();
        //删除之前判断该护理员是否存在护理老人
        int count = ylNursingRecordMapper.selectCount(new QueryWrapper<YlNursingRecord>().eq("STAFF_NO", staffNo).eq("is_effective", 1));
        if (count > 0) {
            map.put("code", "101");
            map.put("msg", "该护理员已绑定老人，不能删除");
            return map;
        }
        YlStaffInfo staffInfo = ylStaffInfoMapper.selectById(staffNo);
        staffInfo.setStaffState("9");
        ylStaffInfoMapper.updateById(staffInfo);
        map.put("code", "200");
        map.put("msg", "删除成功");
        return map;
    }

    @Override
    public List<ExcelStaffinfo> getExportExcelData(YlStaffInfoVo ylStaffInfoVo) throws Exception {
        List<ExcelStaffinfo> excelStaffinfos = new ArrayList<>();
        QueryWrapper<YlStaffInfo> queryWrapper = new QueryWrapper<YlStaffInfo>().eq("STAFF_STATE", 1).eq("PENSION_NO", ylStaffInfoVo.getCurrentPensionNo());
        setQueryWrapper(ylStaffInfoVo, queryWrapper);
        List<YlStaffInfo> ylStaffInfos = ylStaffInfoMapper.selectList(queryWrapper);
        int num = 1;
        for (YlStaffInfo ylStaffInfo : ylStaffInfos) {
            ExcelStaffinfo excelStaffinfo = new ExcelStaffinfo();
            excelStaffinfo.setNum(num++);
            excelStaffinfo.setStaffName(ylStaffInfo.getStaffName());
            //性别
            if ("1".equals(ylStaffInfo.getSex())) {
                excelStaffinfo.setSex("男");
            } else if ("2".equals(ylStaffInfo.getSex())) {
                excelStaffinfo.setSex("女");
            } else {
                excelStaffinfo.setSex("未知");
            }
            //岗位情况
            if ("101".equals(ylStaffInfo.getJobState())) {
                excelStaffinfo.setJobState("在职");
            } else if ("102".equals(ylStaffInfo.getJobState())) {
                excelStaffinfo.setJobState("返聘");
            } else {
                excelStaffinfo.setJobState("兼职");
            }
            //备注
            excelStaffinfo.setRemarks(ylStaffInfo.getRemarks());
            //学历
            if ("2101".equals(ylStaffInfo.getEducation())) {
                excelStaffinfo.setEducation("文盲");
            } else if ("2102".equals(ylStaffInfo.getEducation())) {
                excelStaffinfo.setEducation("小学");
            } else if ("2103".equals(ylStaffInfo.getEducation())) {
                excelStaffinfo.setEducation("初中");
            } else if ("2104".equals(ylStaffInfo.getEducation())) {
                excelStaffinfo.setEducation("中专");
            } else if ("2105".equals(ylStaffInfo.getEducation())) {
                excelStaffinfo.setEducation("高中");
            } else if ("2106".equals(ylStaffInfo.getEducation())) {
                excelStaffinfo.setEducation("大学");
            } else if ("2107".equals(ylStaffInfo.getEducation())) {
                excelStaffinfo.setEducation("硕士");
            } else {
                excelStaffinfo.setEducation("博士");
            }
            excelStaffinfo.setPositional(ylStaffInfo.getPositional());
            excelStaffinfo.setPost(ylStaffInfo.getPost());
            excelStaffinfo.setIdCard(AesMyUtil.Decrypt(ylStaffInfo.getIdCard()));
            excelStaffinfo.setPhone(AesMyUtil.Decrypt(ylStaffInfo.getPhone()));
            excelStaffinfo.setInDate(ylStaffInfo.getInDate());
            excelStaffinfo.setWorkAge(ylStaffInfo.getWorkAge());
            excelStaffinfo.setAge(ylStaffInfo.getAge());
            if (ylStaffInfo.getAge() < 35) {
                excelStaffinfo.setAgeLabel1("✓");
                excelStaffinfo.setAgeLabel2("");
                excelStaffinfo.setAgeLabel3("");
            } else if (ylStaffInfo.getAge() > 35 && ylStaffInfo.getAge() < 46) {
                excelStaffinfo.setAgeLabel1("");
                excelStaffinfo.setAgeLabel3("");
                excelStaffinfo.setAgeLabel2("✓");
            } else if (ylStaffInfo.getAge() > 45) {
                excelStaffinfo.setAgeLabel1("");
                excelStaffinfo.setAgeLabel2("");
                excelStaffinfo.setAgeLabel3("✓");
            }
            excelStaffinfos.add(excelStaffinfo);
        }
        return excelStaffinfos;
    }

    /**
     * 设置查询条件
     * <p>
     * param ylStaffInfoVo
     * param queryWrapper
     */
    private void setQueryWrapper(YlStaffInfoVo ylStaffInfoVo, QueryWrapper<YlStaffInfo> queryWrapper) throws Exception {
        if (Util.isNotNull(ylStaffInfoVo.getStaffName())) {
            queryWrapper.like("STAFF_NAME", ylStaffInfoVo.getStaffName());
        }

        if (Util.isNotNull(ylStaffInfoVo.getIdCard())) {
            queryWrapper.eq("ID_CARD", AesMyUtil.Encrypt(ylStaffInfoVo.getIdCard()));
        }

        if (Util.isNotNull(ylStaffInfoVo.getPhone())) {
            queryWrapper.eq("PHONE", AesMyUtil.Encrypt(ylStaffInfoVo.getPhone()));
        }

        if (Util.isNotNull(ylStaffInfoVo.getAge())) {
            queryWrapper.eq("age", ylStaffInfoVo.getAge());
        }

        if (Util.isNotNull(ylStaffInfoVo.getPositional())) {
            queryWrapper.like("POSITIONAL", ylStaffInfoVo.getPositional());
        }

        if (Util.isNotNull(ylStaffInfoVo.getPost())) {
            queryWrapper.like("post", ylStaffInfoVo.getPost());
        }

        if (Util.isNotNull(ylStaffInfoVo.getSex())) {
            queryWrapper.eq("sex", ylStaffInfoVo.getSex());
        }

        if (Util.isNotNull(ylStaffInfoVo.getEducation())) {
            queryWrapper.eq("education", ylStaffInfoVo.getEducation());
        }

        if (Util.isNotNull(ylStaffInfoVo.getInDate())) {
            queryWrapper.eq("IN_DATE", ylStaffInfoVo.getInDate());
        }

        //年龄条件 0 小于35 1 36-45 2 大于45
        String age1 = "0";
        String age2 = "1";
        String age3 = "2";
        if (age1.equals(ylStaffInfoVo.getAgelabal())) {
            queryWrapper.lt("age", 35);
        } else if (age2.equals(ylStaffInfoVo.getAgelabal())) {
            queryWrapper.between("age", 35, 45);
        } else if (age3.equals(ylStaffInfoVo.getAgelabal())) {
            queryWrapper.gt("age", 45);
        }
        queryWrapper.orderByDesc("CREATE_TIM");
    }

    @Override
    public void changeStaffInfo() throws Exception {
        QueryWrapper<YlStaffInfo> staffInfoQueryWrapper = new QueryWrapper<>();
        staffInfoQueryWrapper.eq("AGE","0");
        List<YlStaffInfo> staffInfoList = ylStaffInfoMapper.selectList(staffInfoQueryWrapper);

        for (YlStaffInfo s:staffInfoList
             ) {
            if(null!=s){
                //根据身份证号计算年龄
                String birthDate = s.getIdCard().substring(6,14);
                int age = Constance.getAgeByBirthDate(birthDate);
                System.out.println(birthDate);
                s.setAge(age);
                System.out.println(age);
                s.setIdCard(AesMyUtil.Encrypt(s.getIdCard()));
                s.setPhone(AesMyUtil.Encrypt(s.getPhone()));
                s.setCreateTim(new Date());
                ylStaffInfoMapper.updateById(s);
            }
        }
    }
}
